'use strict'
const nunjucks = require('nunjucks');

var createEnv = function (path, opst) {
    path = path || 'views';
    opst = opst || {};

    var opstOption = {
        autoescape: opst.autoescape === undefined ? true : opst.autoescape,
        throwOnUndefined: opst.throwOnUndefined === undefined ? false : opst.throwOnUndefined,
        trimBlocks: opst.trimBlocks === undefined ? false : opst.trimBlocks,
        lstripBlocks: opst.lstripBlocks === undefined ? false : opst.lstripBlocks,
        watch: opst.watch === undefined ? false : opst.watch,
        noCache: opst.noCache === undefined ? false : opst.noCache
    }

    var env = nunjucks.configure(path, opstOption);
    return env;
}


module.exports = async (ctx, next) => {
    ctx.render = function (views, model) {
        var env = createEnv('views');
        ctx.response.body = env.render(views, model);
    }
    await next();
}